﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common.Helper;
using Dln.Model;
using Dln.Common;

public partial class Page_DutyAction_WorkInfo : BasePage
{
    protected string openid = "oF6dUwxAa6BtWPqaRnE_lz7lDBgQ";
    protected void Page_Load(object sender, EventArgs e)
    {
        //var code = DNTRequest.GetString("code");
        //if (!string.IsNullOrWhiteSpace(code))
        //{
        //    openid = GetOpenId(code);
        //    if (!string.IsNullOrEmpty(openid)) setLoginCookieOpenId(openid);
        //}
        //else
        //{
        //    HttpCookie userCookies = HttpContext.Current.Request.Cookies["CookiesOpenid"];
        //    if (userCookies != null && !String.IsNullOrEmpty(userCookies.ToString()) && !String.IsNullOrEmpty(userCookies["OpenId"]))
        //    {
        //        openid = userCookies["OpenId"].ToString();
        //    }
        //    else
        //    {
        //        string strUrl = Request.RawUrl.ToString();
        //        string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}%2fpage%2fCommon%2fWeixinRequestPage.aspx%3ffrom%3d{2}&response_type=code&scope=snsapi_userinfo&state=workInfo#wechat_redirect ", AppId, Server.UrlEncode(CommonHelper.GetAppSettings("mainurl")), Server.UrlEncode(strUrl));
        //        Response.Redirect(url, true);
        //    }

        //}
    }

    protected string GetOpenId(string code)
    {
        string tokenurl = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppId, AppSecret, code);
        string json = RequestHelper.SendGet(tokenurl);
        if (!String.IsNullOrEmpty(json))
        {
            openid = json;
            ClientScript.RegisterStartupScript(this.GetType(), " ", "alert('" + json + "');", true);

            try
            {
                var token = DynamicJson.Parse(json);
                return token.openid;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        return null;
    }
}